home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Symantec Visual Cafe for Java 2.5
/
symantec-visual-cafe-2.5-database-dev-edition.iso
/
Visual Cafe Pro v1.0
/
SOURCE.BIN
/
ImageListBox.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
Macintosh to JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
Java Source
|
1997-06-19
|
75.9 KB
|
2,500 lines
package symantec.itools.awt;
import java.awt.Panel;
import java.awt.Dimension;
import java.awt.Component;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Event;
import java.awt.Scrollbar;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Color;
import java.awt.Rectangle;
import java.util.Vector;
import java.awt.image.MemoryImageSource;
/**
* Creates a box containing a list of images.
* <p>
* Use a ImageListBox to display a set of images that the user can select.
* The user cannot type a selection in a list box.
* <p>
* @version 1.0, Nov 26, 1996
* @author Symantec
*/
// 03/02/97 RKM In ctor, make certain bar size is correct for Macs
// 03/21/97 RKM Removed repaint hack from paint
public class ImageListBox
extends Panel
{
//--------------------------------------------------
// constants
//--------------------------------------------------
/**
* Defines the Event ID for when the image portion of a list item
* is selected.
*/
public static final int EVT_IMAGE_SELECT = 0x4004;
/**
* Defines the "regular" border style.
* @see #setBorderType
* @see #getBorderType
*/
public static final int BORDER_REGULAR = 0;
/**
* Defines "no border" style.
* @see #setBorderType
* @see #getBorderType
*/
public static final int BORDER_NONE = 1;
/**
* Defines the width of displayed images, in pixels.
*/
public static final int IMAGE_WIDTH = 19;
private final int LINE_SLOP = 6;
//--------------------------------------------------
// class variables
//--------------------------------------------------
//--------------------------------------------------
// member variables
//--------------------------------------------------
/**
* Zero-relative index of the item displayed in the top row.
*/
protected int nTopRow = 0;
/**
* Width of the list content portion of this component, in pixels.
*/
protected int lWidth = 0;
/**
* Height of a single list cell, in pixels.
*/
protected int cellHt = 0;
/**
* Top margin before the contents of the first list item, in pixels.
*/
protected int yAdj = 6;
/**
* Width of the left and right borders combined, in pixels.
*/
protected int borderWidth = 4;
/**
* Width of the left or right border, in pixels.
*/
protected int halfBorderWidth = 2;
private Component ilbParent;
private String ilbLabel;
private Image SelectImage;
private Scrollbar VBar = null;
private Scrollbar HBar = null;
private Vector items;
private Font font;
private FontMetrics fm = null;
private boolean bAllDirty = true;
private boolean bMultipleSelections = false;
private boolean bBlockPaint = false;
private boolean bVBarVisible = false;
private boolean bHBarVisible = false;
private boolean bInternalBlockPaint = false;
private int rowsToShow = -1;
private int colsToShow = 10;
private int visibleRows = -1;
private int visibleCols = -1;
private int visibleIndex = -1;
private int fontHeight;
private int fontDescent;
private int lastDownModifiers = -1;
private int lastSelected = -1;
private int lastIndex = -1;
private int lastTempIndex = -1;
private int countSelected = 0;
private int borderType = 0;
private int longestLineValue = 0;
private int barSize = 15;
private int xCoord = 0;
private long prevSelectTime = -1;
private long prevSelectRow = -1;
private boolean bOsFlag;
private boolean bComboMode = false;
private boolean bCellBorders = false;
private boolean bAllowShowVBar = true;
private boolean bAllowShowHBar = true;
private Color enabledColor = Color.black;
private Color disabledColor = Color.gray;
private int fastDownCount = 0;
private boolean bMouseDrawHandled = false;
private boolean bDirectNotify = true;
//--------------------------------------------------
// constructors
//--------------------------------------------------
/**
* Construct a default ImageListBox.
* A default ImageListBox has no name label, will show the maximum number of
* visible rows, and allows single list item selection.
*/
public ImageListBox()
{
this(null, "", -1, false);
}
/**
* Construct an ImageListBox with the specified name label.
* It will show the maximum number of visible rows, and allows single list
* item selection.
* @param label the name label of the list box
*/
public ImageListBox(String label)
{
this(null, label, -1, false);
}
/**
* Construct an ImageListBox with the specified name label and
* conditionally allow multiple selections.
* It will show the maximum number of visible rows.
* @param label the name label of the list box
* @param bMultipleSelections if true then multiple selections are allowed;
* if false only single item selections are allowed
*/
public ImageListBox(String label, boolean bMultipleSelections)
{
this(null, label, -1, bMultipleSelections);
}
/**
* Construct an ImageListBox with the specified name label, the
* specified number of visible rows, and conditionally allow multiple
* list item selection.
* @param label the name label of the list box
* @param rows the number of row items to show. If rows is less than or equal
* to zero, the maximum possible number of rows will be shown
* @param bMultipleSelections if true then multiple selections are allowed;
* if false only single item selections are allowed
*/
public ImageListBox(String label, int rows, boolean bMultipleSelections)
{
this(null, label, rows, bMultipleSelections);
}
/**
* Construct an ImageListBox with a specific parent component and the
* specified name label. It will show the maximum visible rows and
* allowing single list item selection.
* @param parent the parent of the list box, where all events are sent
* @param label the name label of the list box
*/
public ImageListBox(Component parent, String label)
{
this(parent, label, -1, false);
}
/**
* Construct an ImageListBox.
* The created ImageListBox will have a specific parent component,
* the specified name label, show a specified number of rows and
* conditionally allow multiple list item selection.
* If the specified parent is null, the
* parent (for event passing purposes) will be determined at event time.
* @param parent the parent of the list box, where all events should be sent
* @param slabel the name label of the list box
* @param rows the number of items to show. If rows is less then or equal
* to zero, the maximum possible number of rows will be shown
* @param bMultipleSelections if true then multiple selections are allowed;
* if false only single item selections are allowed
*/
public ImageListBox(Component parent, String slabel, int rows, boolean bMultipleSelections)
{
if (!System.getProperty("os.name").startsWith("S")) // SunOS, Solaris
{
bOsFlag = false;
if (symantec.itools.lang.OS.isMacintosh())
barSize = 16;
else
barSize = 15;
}
else
{
bOsFlag = true;
barSize = 17;
}
items = new Vector();
ilbParent = parent;
ilbLabel = slabel;
rowsToShow = rows;
this.bMultipleSelections = bMultipleSelections;
// Create select image
int w = 20;
int h = 20;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
pix[index++] = (255 << 24) | 128;
}
}
SelectImage = createImage(new MemoryImageSource(w, h, pix, 0, w));
setLayout(null);
setBackground(Color.white);
VBar = new Scrollbar();
VBar.setBackground(Color.lightGray);
add(VBar);
HBar = new Scrollbar(Scrollbar.HORIZONTAL);
HBar.setBackground(Color.lightGray);
add(HBar);
setFont(new Font("Helvetica", java.awt.Font.PLAIN, 12));
}
//--------------------------------------------------
// accessor methods
//--------------------------------------------------
/**
* Sets ImageListBox "ComboBox Mode".
* This only applies when the ImageListBox is not allowing multiple
* selections. "ComboBox Mode" allows the selection point to follow
* the mouse even when the mouse button is not down.
* @param cond true for "ComboBox Mode", false for normal list box mode.
* @see #getComboMode
*/
public void setComboMode(boolean cond)
{
bComboMode = cond;
yAdj = 6;
invalidate();
}
/**
* Returns the current ImageListBox "ComboBox Mode" setting.
* @see #setComboMode
*/
public boolean getComboMode()
{
return bComboMode;
}
/**
* Sets the number of rows to display in the ImageListBox.
* @param rows number of rows to display
* @see #getRowsToShow
*/
public void setRowsToShow(int rows)
{
rowsToShow = rows;
invalidate();
}
/**
* Returns the current number of rows to display value.
* @see #setRowsToShow
*/
public int getRowsToShow()
{
return rowsToShow;
}
/**
* Sets the vertical scrollbar visibility flag.
* @param cond if true, the vertical scrollbar will be made
* visible when necessary; if false, the vertical scrollbar
* will never be made visible
* @see #getShowVerticalScroll
*/
public void setShowVerticalScroll(boolean cond)
{
if (bAllowShowVBar != cond)
{
bAllowShowVBar = cond;
invalidate();
}
}
/**
* Returns the current vertical scrollbar visibility flag.
* @see #setShowVerticalScroll
*/
public boolean getShowVerticalScroll()
{
return bAllowShowVBar;
}
/**
* Sets the horizontal scrollbar visibility flag.
* @param cond if true, the horizontal scrollbar will be made
* visible when necessary; if false, the horizontal scrollbar
* will never be made visible
* @see #getShowHorizontalScroll
*/
public void setShowHorizontalScroll(boolean cond)
{
if (bAllowShowHBar != cond)
{
bAllowShowHBar = cond;
invalidate();
}
}
/**
* Returns the current horizontal scrollbar visibility flag.
* @see #setShowHorizontalScroll
*/
public boolean getShowHorizontalScroll()
{
return bAllowShowHBar;
}
/**
* Sets the border type of the ImageListBox.
* @param type new border type
* @see #getBorderType
* @see #BORDER_REGULAR
* @see #BORDER_NONE
*/
public void setBorderType(int type)
{
borderType = type;
if (type == BORDER_REGULAR)
{
borderWidth = 4;
halfBorderWidth = 2;
}
else
{
borderWidth = 0;
halfBorderWidth = 0;
}
invalidate();
}
/**
* Returns the current border type of the ImageListBox.
* @see #setBorderType
* @see #BORDER_REGULAR
* @see #BORDER_NONE
*/
public int getBorderType()
{
return borderType;
}
/**
* Conditionally show cell borders for a specific index.
* @param cond true to show the cell borders; false to
* not show the cell borders
* @see #setCellBorders
*/
public void setCellBorder(int index, boolean bOn)
{
if (validIndex(index))
{
ListItem li = (ListItem) items.elementAt(index);
if (li.bCellBorder != bOn)
{
li.bCellBorder = bOn;
li.bDirty = true;
invalidate();
}
}
}
/**
* Sets the ImageListBox cell border display default, and
* resets all items to have the border conditionally.
* @param cond true for "default grid lines on" mode,
* false for "default grid lines off" mode.
* @see #setCellBorder
* @see #getCellBorders
*/
public void setCellBorders(boolean bOn)
{
bCellBorders = bOn;
int s = items.size();
ListItem li = null;
for (int x = 0; x < s; x++)
{
li = (ListItem) items.elementAt(x);
li.bCellBorder = bOn;
}
bAllDirty = true;
invalidate();
}
/**
* Gets the current cell border display mode.
* @return boolean - true for "default grid lines on" mode,
* false for "default grid lines off" mode.
* @see #setCellBorders
*/
public boolean getCellBorders()
{
return bCellBorders;
}
/**
* Set the default color for enabled text items.
* @param color the new default text color
* @see #getDefaultEnabledTextColor
*/
public void setDefaultEnabledTextColor(Color color)
{
enabledColor = color;
invalidate();
}
/**
* Returns the current default enabled text color.
* @see #setDefaultEnabledTextColor
*/
public Color getDefaultEnabledTextColor()
{
return enabledColor;
}
/**
* Set the default color for disabled text items.
* @param color the new default disabled text color
* @see #getDisabledTextColor
*/
public void setDisabledTextColor(Color color)
{
disabledColor = color;
invalidate();
}
/**
* Returns the current default disabled text color.
* @return Color - current default disabled text color
* @see #setDisabledTextColor
*/
public Color getDisabledTextColor()
{
return disabledColor;
}
/**
* Set the text color for an item at the given index.
* @param index the zero-relative index of the item
* @param color the color, null to use default color
*/
public void setEnabledTextColor(int index, Color color)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
tempItem.color = color;
}
}
//--------------------------------------------------
// event methods
//--------------------------------------------------
/**
* Processes events for this component.
* This is a standard Java AWT method which gets called by the AWT
* to handle this component's events. The default handler for
* components dispatches to one of the following methods as needed:
* action(), gotFocus(), lostFocus(), keyDown(), keyUp(), mouseEnter(),
* mouseExit(), mouseMove(), mouseDrag(), mouseDown(), or mouseUp().
*
* @param evt the event to handle
* @return true if the event was handled and no further action is needed,
* false to pass the event to this component's parent
* @see java.awt.Component#action
* @see java.awt.Component#gotFocus
* @see java.awt.Component#lostFocus
* @see java.awt.Component#keyDown
* @see java.awt.Component#keyUp
* @see java.awt.Component#mouseEnter
* @see java.awt.Component#mouseExit
* @see java.awt.Component#mouseMove
* @see java.awt.Component#mouseDrag
* @see java.awt.Component#mouseDown
* @see java.awt.Component#mouseUp
*/
public boolean handleEvent(Event evt)
{
long selectTime;
boolean bDoubleClick;
int index = -1;
String sIndex = "";
switch( evt.id )
{
case Event.MOUSE_DOWN:
fastDownCount++;
bMouseDrawHandled = false;
lastDownModifiers = evt.modifiers;
if ( (evt.x > -1) && (evt.x < lWidth) )
{
index = mouseCalcIndex(evt.y);
if (index == -1)
return true;
if (bMultipleSelections)
{
switch (lastDownModifiers)
{
case 1: // shift
bMouseDrawHandled = true;
shiftSelect(index, false);
break;
case 2: // ctrl
bMouseDrawHandled = true;
ctrlSelect(index);
break;
case 3: // ctrl & shift
bMouseDrawHandled = true;
shiftSelect(index, true);
break;
default:
select(index);
break;
}
repaint();
}
else
{
lastTempIndex = index;
if (lastSelected == index)
return true;
select(index);
repaint();
}
}
return true;
case Event.MOUSE_MOVE:
if (bMultipleSelections)
break;
// otherwise continue as MOUSE_DRAG
case Event.MOUSE_DRAG:
if ( (evt.x > -1) && (evt.x < lWidth) )
{
if (bComboMode)
{
index = mouseCalcIndex(evt.y);
if (index == -1 || index == lastIndex)
return true;
lastIndex = index;
select(index);
repaint();
}
else if (evt.id == Event.MOUSE_DRAG)
{
index = mouseCalcIndex(evt.y);
if (index != -1 && index != lastIndex)
{
if (bMultipleSelections)
{
if (lastDownModifiers != 0)
break;
bInternalBlockPaint = true;
makeVisible(index);
bInternalBlockPaint = false;
shiftSelect(index, false);
}
else
select(index);
lastIndex = index;
}
}
}
break;
case Event.MOUSE_EXIT:
if (!bMultipleSelections && bComboMode)
lastIndex = -1;
break;
case Event.KEY_ACTION:
if (evt.modifiers > 1)
break;
switch(evt.key)
{
case Event.HOME: index = keyCalcIndex(0, true); break;
case Event.END: index = keyCalcIndex(items.size() - 1, true); break;
case Event.PGUP: index = keyCalcIndex(-visibleRows, false); break;
case Event.PGDN: index = keyCalcIndex(visibleRows, false); break;
case Event.UP: index = keyCalcIndex(-1, false); break;
case Event.DOWN: index = keyCalcIndex(1, false); break;
}
if (index == -1)
break;
if (bMultipleSelections)
{
switch (evt.modifiers)
{
case 0: // none, regular select
break;
case 1: // shift
makeVisible(index);
shiftSelect(index, false);
return true;
default:
return true;
}
}
// regular select, multiple or single
select(index);
repaint();
evt.x = -1;
notifyParent(evt, index, false);
prevSelectTime = -1;
prevSelectRow = -1;
return true;
case Event.MOUSE_UP:
fastDownCount--;
if (bMouseDrawHandled) // multi-selected ctrl or shift
{
evt.x = -1;
notifyParent(evt, -1, false);
return true;
}
if (!bMultipleSelections)
{
index = mouseCalcIndex(evt.y);
if ( (index == -1) || (evt.x < 0) || (evt.x >= lWidth) )
{
fastDownCount = 0;
index = lastTempIndex;
if (index == -1)
return true;
}
}
if (bComboMode)
{
fastDownCount = 0;
bDoubleClick = false;
}
else
{
// sometimes you get two MOUSE_DOWNS before the first MOUSE_UP
if (fastDownCount > 0)
{
fastDownCount = 0;
bDoubleClick = true;
prevSelectTime = -1;
prevSelectRow = -1;
}
else
{
selectTime = System.currentTimeMillis();
if ( (prevSelectTime != -1) && // valid prevSelectTime
(prevSelectRow == index) && //
((prevSelectTime + 250) > selectTime) // A double click
)
{
bDoubleClick = true;
prevSelectTime = -1;
prevSelectRow = -1;
}
else
{
bDoubleClick = false;
prevSelectTime = selectTime;
prevSelectRow = index;
}
}
}
if (lastSelected != index)
{
select(index);
repaint();
}
if (bMultipleSelections)
{
notifyParent(evt, lastSelected, bDoubleClick);
lastIndex = lastSelected;
}
else
{
notifyParent(evt, index, bDoubleClick);
lastIndex = index;
}
lastTempIndex = -1;
lastDownModifiers = -1;
return true;
case Event.SCROLL_PAGE_DOWN: if (evt.target == VBar) scrollVertical(evt.id, false); else scrollHorizontal(evt.id, false); break;
case Event.SCROLL_PAGE_UP: if (evt.target == VBar) scrollVertical(evt.id, false); else scrollHorizontal(evt.id, false); break;
case Event.SCROLL_LINE_DOWN: if (evt.target == VBar) scrollVertical(evt.id, false); else scrollHorizontal(evt.id, false); break;
case Event.SCROLL_LINE_UP: if (evt.target == VBar) scrollVertical(evt.id, false); else scrollHorizontal(evt.id, false); break;
case Event.SCROLL_ABSOLUTE: if (evt.target == VBar) scrollVertical(((Integer)evt.arg).intValue(), true); else scrollHorizontal(((Integer)evt.arg).intValue(), true); break;
}
return false;
}
//--------------------------------------------------
// class methods
//--------------------------------------------------
//--------------------------------------------------
// member methods
//--------------------------------------------------
/**
* Sets the mode of the ImageListBox to "MultiColumnListBox
* Mode". This method always sets the border type to BORDER_NONE
* and conditionally sets whether the grid should be displayed.
* @param bCellBorders true to display the cell grid,
* false to not display the grid
* @see #BORDER_NONE
*/
public synchronized void setMultiColumnMode(boolean bCellBorders)
{
bComboMode = false;
yAdj = 2;
setBorderType(BORDER_NONE);
setCellBorders(bCellBorders);
}
/**
* Adds an item to the end of the list and enables the item.
* @param item the item to be added
* @see #setListItems(java.lang.String[])
* @see #addItem(java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String)
* @see #addItem(java.awt.Image, java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String, boolean, java.awt.Color)
*/
public synchronized void addItem(String item)
{
addItem(new ListItem((Image)null, item, true, fm, bCellBorders));
}
/**
* Adds the string array to the list.
* @param items items to add to the list
* @see #getListItems
* @see #setListItems(java.lang.String[])
* @see #addItem(java.lang.String)
* @see #addItem(java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String)
* @see #addItem(java.awt.Image, java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String, boolean, java.awt.Color)
*/
public void setListItems(String[] items)
{
clear();
for (int i = 0; i < items.length; ++i)
{
addItem(items[i]);
}
}
/**
* Returns the current list as a array.
* @return String[] current list
* @see #setListItems
*/
public String[] getListItems()
{
int len = countItems();
String[] items = new String[len];
for (int i = 0; i < len; ++i)
{
items[i] = getItem(i);
}
return items;
}
/**
* Adds an item to the end of the list and conditionally enables
* it.
* @param item the item to be added
* @param bEnabled if true, enable the item; if false, disable
* the item
* @see #setListItems(java.lang.String[])
* @see #addItem(java.lang.String)
* @see #addItem(java.awt.Image, java.lang.String)
* @see #addItem(java.awt.Image, java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String, boolean, java.awt.Color)
*/
public synchronized void addItem(String item, boolean bEnabled)
{
addItem(new ListItem((Image)null, item, bEnabled, fm, bCellBorders));
}
/**
* Adds an item with an image to the end of the list and enables
* it.
* @param image the image to display on the item line
* @param item the item to be added
* @see #setListItems(java.lang.String[])
* @see #addItem(java.lang.String)
* @see #addItem(java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String, boolean, java.awt.Color)
*/
public synchronized void addItem(Image image, String item)
{
addItem(new ListItem(image, item, true, fm, bCellBorders));
}
/**
* Adds an item with an image to the end of the list and
* conditionally enables it.
* @param image the image to display on item line
* @param item the item to be added
* @param bEnabled if true, enable the item; if false, disable the item
* @see #setListItems(java.lang.String[])
* @see #addItem(java.lang.String)
* @see #addItem(java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String)
* @see #addItem(java.awt.Image, java.lang.String, boolean, java.awt.Color)
*/
public synchronized void addItem(Image image, String item, boolean bEnabled)
{
addItem(new ListItem(image, item, bEnabled, fm, bCellBorders));
}
/**
* Adds an item with an image to the end of the list and
* conditionally enables it.
* @param image the image to display on item line
* @param item the item to be added
* @param bEnabled if true, enable the item; if false, disable the item
* @param color text color of item
* @see #setListItems(java.lang.String[])
* @see #addItem(java.lang.String)
* @see #addItem(java.lang.String, boolean)
* @see #addItem(java.awt.Image, java.lang.String)
* @see #addItem(java.awt.Image, java.lang.String, boolean)
*/
public synchronized void addItem(Image image, String item, boolean bEnabled, Color color)
{
ListItem li = new ListItem(image, item, bEnabled, fm, bCellBorders);
li.color = color;
addItem(li);
}
private void addItem(ListItem li)
{
items.addElement(li);
updateWidth(li);
if (!bInternalBlockPaint) repaint();
}
/**
* Inserts an item with an image at a specific zero-relative index and
* conditionally enables it.
* @param index the zero-relative index to insert at
* @param image the image to display on the item line
* @param item the item to be added
* @param bEnabled if true, enable the item; if false, disable the item
*/
public synchronized void insertItem(int index, Image image, String item, boolean bEnabled)
{
ListItem li = null;
if ( validIndex(index) )
{
items.insertElementAt(li = new ListItem(image, item, bEnabled, fm, bCellBorders), index);
bAllDirty = true;
if (lastSelected >= index)
lastSelected++;
}
else
items.addElement(li = new ListItem(image, item, bEnabled, fm, bCellBorders));
updateWidth(li);
if (!bInternalBlockPaint) repaint();
}
/**
* Inserts a vector list of items at a specific zero-relative index and
* conditionally enables them.
* @param index the zero-relative index to insert at
* @param itemVector the item vector to be added
* @param image the image to display for all inserted items
* @param bEnabled if true, enable the items; if false, disable the items
*/
public synchronized void insertItems(int index, Vector itemVector, Image image, boolean bEnabled)
{
int s = itemVector.size();
int x = 0;
int idx = index;
if ( validIndex(index) )
{
for (x = 0; x < s; x++)
items.insertElementAt(new ListItem(image, (String) itemVector.elementAt(x), bEnabled, fm, bCellBorders), index++);
}
else
{
for (x = 0; x < s; x++)
items.addElement(new ListItem(image, (String) itemVector.elementAt(x), bEnabled, fm, bCellBorders));
}
deselectAll();
bAllDirty = true;
updateWidths(fm);
if (!bInternalBlockPaint) repaint();
}
/**
* Returns the number of items in the list.
* @return number of items in the list
* @see #getItem
*/
public int countItems()
{
return items.size();
}
/**
* Gets the item associated with the specified zero-relative index.
* @param index the zero-relative position of the item
* @return item text, or null if index is invalid
* @see #countItems
*/
public String getItem(int index)
{
if (validIndex(index))
return ((ListItem)items.elementAt(index)).sText;
else
return null;
}
/**
* Gets the image associated with the specified zero-relative index.
* @param index the zero-relative position of the item
* @return image at index, or null if index is
* invalid
* @see #countItems
*/
public Image getImage(int index)
{
if (validIndex(index))
return ((ListItem)items.elementAt(index)).image;
else
return null;
}
/**
* Change the image associated with an item at a
* specific zero-relative index.
* @param index the zero-relative position of the item
* @param image the image to associate with an item
*/
public void changeImage(int index, Image image)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
tempItem.image = image;
tempItem.bDirty = true;
if (!bInternalBlockPaint) repaint();
}
}
/**
* Change the text associated with an item at a
* specific zero-relative index.
* @param index the zero-relative position of the item
* @param text the text to associate with an item
*/
public void changeText(int index, String text)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
tempItem.sText = text;
tempItem.bDirty = true;
tempItem.updateWidth(fm);
updateWidth(tempItem);
if (!bInternalBlockPaint) repaint();
}
}
/**
* Set a flag indicating that the item at the given zero-relative index
* has been edited.
* @param index the zero-relative position of the item
* @param bCond "edited" flag
* @see #getEdited
*/
public void setEdited(int index, boolean bCond)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
tempItem.bEdited = bCond;
}
}
/**
* Get the "edited" flag state for an item at the given
* zero-relative index.
* @param index the zero-relative position of the item
* @return true if flagged as edited, false if not
* @see #setEdited
*/
public boolean getEdited(int index)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
return tempItem.bEdited;
}
return false;
}
/**
* Enables an item in the list.
* @param index the zero-relative position of the item
* @see #disable
*/
public void enable(int index)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
if (!tempItem.bEnabled)
{
tempItem.bEnabled = true;
tempItem.bDirty = true;
if (!bInternalBlockPaint) repaint();
}
}
}
/**
* Disables an item in the list.
* @param index the zero-relative position of the item
* @see #enable
*/
public void disable(int index)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
if (tempItem.bEnabled)
{
tempItem.bEnabled = false;
tempItem.bDirty = true;
deselect(index);
if (!bInternalBlockPaint) repaint();
}
}
}
/**
* Enable or disable a specific item in the list.
* @param index the zero-relative position of the item
* @param cond if true, enable the item; if false, disable the
* item
* @see #isEnabled
*/
public void enable(int index, boolean cond)
{
if (cond)
enable(index);
else
disable(index);
}
/**
* Get the enabled state of a specific item in the list.
* @param index the zero-relative position of the item in the list
* @return true if the item is enabled, false
* if the item is disabled.
* @see #enable
*/
public boolean isEnabled(int index)
{
if (validIndex(index))
{
ListItem tempItem = ((ListItem)items.elementAt(index));
return tempItem.bEnabled;
}
return false;
}
/**
* Clears the list.
* @see #delItem
* @see #delItems
*/
public synchronized void clear()
{
items = new Vector();
nTopRow = 0;
visibleIndex = -1;
lastDownModifiers = -1;
lastSelected = -1;
lastIndex = -1;
lastTempIndex = -1;
countSelected = 0;
prevSelectTime = -1;
prevSelectRow = -1;
VBar.setValues(1, 1, 0, 2);
VBar.hide();
bVBarVisible = false;
HBar.setValues(1, 1, 0, 2);
HBar.hide();
bHBarVisible = false;
bAllDirty = true;
bInternalBlockPaint = false;
bBlockPaint = false;
longestLineValue = 0;
xCoord = 0;
repaint();
}
/**
* Delete an item from the list.
* @param index the zero-relative index of the item to delete
* @see #delItems
* @see #delSelectedItems
*/
public synchronized void delItem(int index)
{
if (validIndex(index))
{
deselect(index);
items.removeElementAt(index);
bAllDirty = true;
scrollVertical(nTopRow, true);
updateWidths(null);
if (!bInternalBlockPaint) repaint();
}
}
/**
* Delete multiple items from the list.
* Note that the end index must be greater than or
* equal to the start index.
* @param start the zero-relative start index of the items
* @param end the zero-relative end index of the items
* @see #delItem
* @see #delSelectedItems
*/
public synchronized void delItems(int start, int end)
{
int s = items.size();
if (s > 0)
{
if (end >= s)
end = s - 1;
if (start < 0)
start = 0;
if (start <= end)
{
bInternalBlockPaint = true;
for (int i = end; i >= start; i--)
{
deselect(i);
items.removeElementAt(i);
}
bInternalBlockPaint = false;
bAllDirty = true;
scrollVertical(nTopRow, true);
updateWidths(null);
repaint();
}
}
}
/**
* Delete the selected items from the list.
* @see #delItem
* @see #delItems
*/
public synchronized void delSelectedItems()
{
int s = items.size();
bInternalBlockPaint = true;
for (int x = 0; x < s; )
{
ListItem tempItem = (ListItem) items.elementAt(x);
if (tempItem.bSelected)
{
deselect(x);
items.removeElementAt(x);
s--;
}
else
x++;
}
bAllDirty = true;
scrollVertical(nTopRow, true);
updateWidths(null);
bInternalBlockPaint = false;
repaint();
}
/**
* Get the zero-relative index of the selected item in the list
* @return the zero-relative index of selected item, or -1 if no item is
* selected
* @see #select
* @see #deselect
* @see #isSelected
*/
public synchronized int getSelectedIndex()
{
return lastSelected;
}
/**
* Returns the selected indexes in the list.
* @return int[] - array of selected zero-relative indexes
* @see #select
* @see #deselect
* @see #isSelected
*/
public synchronized int[] getSelectedIndexes()
{
int sel[] = new int[countSelected];
if (countSelected == 1)
sel[0] = lastSelected;
else if (countSelected > 1)
{
int x = 0;
int s = items.size();
for (int i = 0; i < s; i++)
{
ListItem tempItem = (ListItem)items.elementAt(i);
if (tempItem.bSelected)
{
sel[x++] = i;
if (x == countSelected)
break;
}
}
}
return sel;
}
/**
* Returns the currently selected item in the list.
* @return the currently selected item, or null if
* no item is selected.
* @see #getSelectedItems
* @see #select
* @see #deselect
* @see #isSelected
*/
public synchronized String getSelectedItem()
{
return (lastSelected < 0) ? null : getItem(lastSelected);
}
/**
* Returns the selected items in the list.
* @return an array of selected items in the list
* @see #getSelectedItem
* @see #select
* @see #deselect
* @see #isSelected
*/
public synchronized String[] getSelectedItems()
{
String str[] = new String[countSelected];
ListItem tempItem;
if (!bMultipleSelections)
{
if (lastSelected != -1)
{
tempItem = (ListItem)items.elementAt(lastSelected);
str[0] = tempItem.sText;
}
}
else if (countSelected > 0)
{
int x = 0;
int s = items.size();
for (int i = 0; i < s; i++)
{
tempItem = (ListItem)items.elementAt(i);
if (tempItem.bSelected)
{
str[x] = tempItem.sText;
if (++x == countSelected)
break;
}
}
}
return str;
}
/**
* Returns the current list box label.
* @return the current list box label
*/
public synchronized String getLabel()
{
return new String(ilbLabel);
}
/**
* Select the item at the specified index.
* This method deselects all others when multiple
* selections not enabled.
* @param index the zero-relative position of the item to select
* @see #getSelectedItem
* @see #deselect
* @see #isSelected
*/
public synchronized void select(int index)
{
if ( isEnabled(index) )
{
ListItem listItem = null;
if (bMultipleSelections)
{
int s = items.size();
for (int i = 0; i < s; i++)
{
listItem = (ListItem)items.elementAt(i);
if (i == index)
{
if (!listItem.bSelected)
{
listItem.bSelected = true;
listItem.bDirty = true;
}
}
else
{
if (listItem.bSelected)
{
listItem.bSelected = false;
listItem.bDirty = true;
}
}
}
}
else
{
if (lastSelected != index)
{
if (lastSelected != -1)
deselect(lastSelected);
listItem = (ListItem)items.elementAt(index);
listItem.bSelected = true;
listItem.bDirty = true;
}
}
lastSelected = index;
countSelected = 1;
makeVisible(index);
}
}
/**
* Selects the first item in the list which has exactly
* matching text.
* @param str the String to select in the list
*/
public synchronized void select(String str)
{
ListItem li;
int z = items.size();
for (int i = 0; i < z; i++)
{
li = (ListItem) items.elementAt(i);
if (li.sText.equals(str))
{
if (li.bEnabled)
select(i);
return;
}
}
}
/**
* Select an index in a multiple-selection-enabled ImageListBox
* using Shift, Ctrl or Ctrl-Shift modifiers to mimic mouse
* selecting with the Shift, Ctrl or Ctrl-Shift modifiers.
* @param index the zero-relative position of the item to select multiple
* @param bShift boolean indicating whether to select with
* Shift modifier
* @param bControl boolean indicating whether to select with
* Ctrl modifier
*/
public synchronized void selectMultiple(int index, boolean bShift, boolean bControl)
{
if (!bMultipleSelections)
{
bShift = false;
bControl = false;
}
if (bShift)
shiftSelect(index, bControl);
else if (bControl)
ctrlSelect(index);
else
select(index);
}
/**
* Selects all items in the list.
* @see #deselectAll
*/
public synchronized void selectAll()
{
if (!bMultipleSelections)
return;
int s = items.size();
ListItem li;
for (int i = 0; i < s; i++)
{
li = (ListItem)items.elementAt(i);
if (li.bEnabled)
li.bSelected = true;
}
countSelected = s;
lastSelected = s - 1;
repaint();
}
/**
* Deselects the item at the specified index.
* @param index the zero-relative position of the item to deselect
* @see #select
* @see #getSelectedItem
* @see #isSelected
*/
public synchronized void deselect(int index)
{
if (validIndex(index))
{
ListItem listItem = (ListItem)items.elementAt(index);
if (listItem.bSelected)
{
if (lastSelected == index)
lastSelected = -1;
listItem.bSelected = false;
listItem.bDirty = true;
countSelected--;
if (!bInternalBlockPaint) repaint();
}
}
}
/**
* Deselects all items in the list.
* @see #selectAll
*/
public synchronized void deselectAll()
{
ListItem li;
if (!bMultipleSelections)
{
if (lastSelected != -1)
{
li = (ListItem)items.elementAt(lastSelected);
li.bSelected = false;
li.bDirty = true;
}
}
else
{
int s = items.size();
for (int i = 0; i < s; i++)
{
li = (ListItem)items.elementAt(i);
if (li.bSelected)
{
li.bSelected = false;
li.bDirty = true;
}
}
}
lastSelected = -1;
countSelected = 0;
repaint();
}
/**
* Get the selected state of the item at the given index.
* @param index the zero-relative index of the item to be checked
* @return true if the item at the specified index
* is selected; false if it is not selected
* @see #select
* @see #deselect
* @see #isSelected
*/
public synchronized boolean isSelected(int index)
{
if (validIndex(index))
{
ListItem listItem = (ListItem)items.elementAt(index);
return listItem.bSelected;
}
return false;
}
/**
* Returns the number of visible lines in this list.
* @return the number of visible lines in the list.
*/
public int getRows()
{
return visibleRows;
}
/**
* Returns true if this list allows multiple selections.
* @return true if multiple selections allowed, false otherwise
* @see #setMultipleSelections
*/
public boolean allowsMultipleSelections()
{
return bMultipleSelections;
}
/**
* Sets whether this list should allow multiple selections
* or not.
* @param cond whether to allow multiple selections
* @see #allowsMultipleSelections
*/
public void setMultipleSelections(boolean cond)
{
if (cond != bMultipleSelections)
{
if (!cond) // deselect first because deselect works differently according to bMultipleSelections flag
deselectAll();
bMultipleSelections = cond;
}
}
/**
* Gets the index of the item that was last made visible by
* the method makeVisible.
* @return the zero-relative index of the item last made visible by the
* makeVisible method
* @see #makeVisible
*/
public int getVisibleIndex()
{
return visibleIndex;
}
/**
* Forces the item at the specified index to be visible in
* the window.
* @param index the zero-relative position of the item
* @see #getVisibleIndex
*/
public synchronized void makeVisible(int index)
{
visibleIndex = index;
if ( nTopRow > index )
scrollVertical(index, true);
else if (index >= (nTopRow + visibleRows))
scrollVertical(index - visibleRows + 1, true);
}
/**
* Forces the item at the specified index to be visible at
* the top of the window.
* @param index the zero-relative position of the item
*/
public void setTopRow(int index)
{
int s = items.size();
if (s < visibleRows)
index = 0;
else if (index > (s - visibleRows))
index = s - visibleRows;
if (nTopRow != index)
{
nTopRow = index;
bAllDirty = true;
invalidate();
}
}
/**
* Returns the preferred dimensions needed for the list with
* the specified amount of rows.
* @param rows number of rows in the list
* @return the preferred of this list with the given the number of rows
*/
/**
* Returns the recommended dimensions to properly display this component.
* This is a standard Java AWT method which gets called to determine
* the recommended size of this component.
*
* @see #minimumSize
*/
public Dimension preferredSize(int rows)
{
Dimension d = minimumSize(rows);
Dimension s = size();
return new Dimension(Math.max(d.width, s.width), Math.max(d.height, s.height));
}
/**
* Returns the preferred dimensions needed for the list.
* @return the preferred size with the specified number of rows if the
* row size is greater than 0.
*/
public Dimension preferredSize()
{
if (rowsToShow > 0)
return preferredSize(rowsToShow);
return preferredSize(items.size());
}
/**
* Returns the minimum dimensions needed for the amount of rows
* in the list.
* @param rows minimum amount of rows in the list
* @return the minimum size of this list with the given the number of rows
*/
public Dimension minimumSize(int rows)
{
font = getFont();
if (font != null)
{
fm = getFontMetrics(font);
if (fm != null)
{
fontHeight = fm.getHeight();
if (bCellBorders)
cellHt = fontHeight+5;
else
cellHt = fontHeight+1;
return new Dimension( ((fm.stringWidth("WN") * colsToShow) / 2) + LINE_SLOP, (rows * cellHt + borderWidth + 4) );
}
}
return new Dimension(borderWidth + LINE_SLOP, borderWidth + 4);
}
/**
* Returns the minimum dimensions to properly display this component.
* This is a standard Java AWT method which gets called to determine
* the minimum size of this component.
* Considerations include the number of rows in the list.
* @see #preferredSize
*/
public Dimension minimumSize()
{
if (rowsToShow > 0)
return minimumSize(rowsToShow);
return minimumSize(items.size());
}
/**
* Tells this component that it has been added to a container.
* This is a standard Java AWT method which gets called by the AWT when
* this component is added to a container. Typically, it is used to
* create this component's peer.
* It is overridden here to handle list metrics after the peer is created.
*
* @see java.awt.Container#removeNotify
*/
public void addNotify()
{
super.addNotify();
font = getFont();
fm = getFontMetrics(font);
fontHeight = fm.getHeight();
updateWidths(fm);
}
/**
* Sets the number of columns to be used by minimumSize().
* @param columns the number of columns
*/
public synchronized void setColumns(int columns)
{
if (colsToShow != columns)
{
colsToShow = columns;
invalidate();
}
}
/**
* Blocks the repainting of the control.
* @param cond conditionally block painting
*/
public synchronized void blockPaint(boolean cond)
{
bBlockPaint = cond;
if (!bBlockPaint)
repaint();
}
/**
* Sets this component's text font.
* This is a standard Java AWT method which gets called to change
* the font used for drawing text in this component.
* In this case the text is in the list box items.
* @param f the new font to use for drawing text
* @see java.awt.Component#getFont
* @see #getListBoxFont
* @see #setListBoxFont
*/
public synchronized void setFont(Font f)
{
if (f == null)
return;
super.setFont(f);
fm = getFontMetrics(f);
updateWidths(fm);
xCoord = 0;
if (bOsFlag)
{
bAllDirty = true;
invalidate();
}
}
/**
* Set the font for the items in the list box. Note: this method is the
* same as setFont().
* @param f new font to use for list box items
* @see #getListBoxFont
*/
public synchronized void setListBoxFont(Font f)
{
setFont(f);
}
/**
* Get the font used for the list box items.
* @see #setListBoxFont
*/
public synchronized Font getListBoxFont()
{
return super.getFont();
}
/**
* Scroll the ImageListBox vertically.
* @param info either an absolute value or Event.SCROLL_PAGE_DOWN,
* Event.SCROLL_PAGE_UP, Event.SCROLL_LINE_DOWN, Event.SCROLL_LINE_UP
* the range for absolute value should be between 0 and number of rows in ImageListBox
* @param bAbsolute whether to treat the info as an absolute value or as one of the constants
*/
public synchronized void scrollVertical(int info, boolean bAbsolute)
{
int temp = nTopRow;
if (visibleRows == 0)
temp = 0;
else
{
if (bAbsolute)
temp = info;
else
{
switch (info)
{
case Event.SCROLL_PAGE_DOWN: temp += visibleRows; break;
case Event.SCROLL_PAGE_UP: temp -= visibleRows; break;
case Event.SCROLL_LINE_DOWN: temp += 1; break;
case Event.SCROLL_LINE_UP: temp -= 1; break;
}
}
if (temp < 0)
temp = 0;
else if (temp > (items.size() - visibleRows))
{
temp = items.size() - visibleRows;
if (temp < 0)
temp = 0;
}
}
if (nTopRow != temp)
{
nTopRow = temp;
VBar.setValue(temp);
bAllDirty = true;
if (!bInternalBlockPaint) repaint();
}
}
/**
* Scroll the ImageListBox horizontally
* @param info either an absolute value or Event.SCROLL_PAGE_DOWN,
* Event.SCROLL_PAGE_UP, Event.SCROLL_LINE_DOWN, Event.SCROLL_LINE_UP
* the range for absolute value should be between 0 pixel width of ImageListBox
* @param bAbsolute whether to treat the info as an absolute value or as one of the constants
*/
public synchronized void scrollHorizontal(int info, boolean bAbsolute)
{
int temp = xCoord;
if (bAbsolute)
temp = -info;
else
{
switch (info)
{
case Event.SCROLL_PAGE_DOWN: temp -= lWidth; break;
case Event.SCROLL_PAGE_UP: temp += lWidth; break;
case Event.SCROLL_LINE_DOWN: temp -= 1; break;
case Event.SCROLL_LINE_UP: temp += 1; break;
}
}
if (temp > 0)
temp = 0;
else if ( (-temp) > HBar.getMaximum())
temp = -HBar.getMaximum();
if (xCoord != temp)
{
xCoord = temp;
HBar.setValue(-temp);
bAllDirty = true;
if (!bInternalBlockPaint) repaint();
}
}
/**
* Handles redrawing of this component on the screen.
* This is a standard Java AWT method which gets called by the Java
* AWT (repaint()) to handle repainting this component on the screen.
* The graphics context clipping region is set to the bounding rectangle
* of this component and its <0,0> coordinate is this component's
* top-left corner.
* Typically this method paints the background color to clear the
* component's drawing space, sets graphics context to be the foreground
* color, and then calls paint() to draw the component.
*
* It is overridden here to allow the blocking of painting.
*
* @param g the graphics context
* @see java.awt.Component#repaint
* @see #paint
*/
public synchronized void update(Graphics g)
{
if (!bBlockPaint && !bInternalBlockPaint)
paint(g);
}
/**
* Paints this component using the given graphics context.
* This is a standard Java AWT method which typically gets called
* by the AWT to handle painting this component. It paints this component
* using the given graphics context. The graphics context clipping region
* is set to the bounding rectangle of this component and its <0,0>
* coordinate is this component's top-left corner.
*
* @param g the graphics context used for painting
* @see java.awt.Component#repaint
* @see #update
*/
public synchronized void paint(Graphics g)
{
ListItem item;
int nItems;
int xSelectStartLoc;
int i, j, rows;
int vWid = 0;
int hHt = 0;
int gridAdj = 0;
int yLoc = 0;
int ySLoc = 0;
boolean bShowV = false;
boolean bShowH = false;
// Size size of ListBox
Rectangle rect = bounds();
font = g.getFont();
fm = g.getFontMetrics(font);
fontHeight = fm.getHeight();
fontDescent = fm.getDescent();
nItems = items.size();
if (bCellBorders)
cellHt = fontHeight+5;
else
cellHt = fontHeight+1;
if (nItems == 0)
{
nTopRow = 0;
visibleRows = 0;
bShowH = false;
bShowV = false;
xCoord = 0;
}
else
{
if ( bAllowShowHBar && (longestLineValue > (rect.width - borderWidth)) )
{
bShowH = true;
hHt = barSize;
}
else
{
bShowH = false;
hHt = 0;
}
rows = (rect.height - hHt - borderWidth - 4) / cellHt;
if ( bAllowShowVBar && (nItems > rows) )
{
bShowV = true;
vWid = barSize;
if (!bShowH)
{
if ( bAllowShowHBar && (longestLineValue > (rect.width - borderWidth - vWid)) )
{
bShowH = true;
hHt = barSize;
rows = (rect.height - hHt - borderWidth - 4) / cellHt;
}
}
}
else
{
bShowV = false;
vWid = 0;
}
if (visibleRows != rows)
{
visibleRows = rows;
bAllDirty = true;
}
if (bShowV)
{
VBar.reshape(rect.width-barSize-halfBorderWidth, halfBorderWidth, barSize, rect.height-borderWidth-hHt);
VBar.setValues(nTopRow, visibleRows, 0, nItems - visibleRows);
VBar.setPageIncrement(visibleRows);
lWidth = rect.width-vWid-borderWidth;
if (!bVBarVisible)
{
bVBarVisible = true;
VBar.show();
}
}
else
{
lWidth = rect.width - borderWidth;
if (bVBarVisible)
{
bVBarVisible = false;
VBar.hide();
}
}
if (bShowH)
{
HBar.reshape(halfBorderWidth, rect.height-barSize-halfBorderWidth, rect.width-borderWidth-vWid, barSize);
HBar.setValues(-xCoord, lWidth, 0, (longestLineValue - lWidth));
HBar.setPageIncrement(lWidth);
if (!bHBarVisible)
{
bHBarVisible = true;
HBar.show();
}
}
else
{
if (bHBarVisible)
{
bHBarVisible = false;
HBar.hide();
}
}
}
if (nItems == 0 || bAllDirty)
g.clearRect(halfBorderWidth, halfBorderWidth, rect.width-borderWidth, rect.height-borderWidth);
if (borderType == 0)
{
g.setColor(Color.black);
g.drawLine(0, 0, rect.width-2, 0);
g.drawLine(0, 0, 0, rect.height-2);
g.setColor(Color.gray);
g.drawLine(1, 1, rect.width-3, 1);
g.drawLine(1, 1, 1, rect.height-3);
g.setColor(Color.lightGray);
g.drawLine(1, rect.height-2, rect.width-2, rect.height-2 );
g.drawLine(rect.width-2, 1, rect.width-2, rect.height-3 );
g.setColor(Color.white);
g.drawLine(0, rect.height-1, rect.width-1, rect.height-1 );
g.drawLine(rect.width-1, 0, rect.width-1, rect.height-1 );
}
g.clipRect(halfBorderWidth, halfBorderWidth, lWidth, rect.height-halfBorderWidth);
if (nTopRow >= nItems)
nTopRow -= visibleRows + 1;
i = nTopRow;
j = Math.min(nTopRow + visibleRows, nItems);
for (int dp = 0; i < j; ++i, ++dp)
{
yLoc = ( (dp) * (cellHt) ) + yAdj;
ySLoc = ( (dp + 1) * (cellHt) ) + yAdj;
item = (ListItem)items.elementAt(i);
if (item.bDirty || bAllDirty)
{
item.bDirty = false;
g.clearRect(halfBorderWidth, yLoc, lWidth, cellHt);
}
if (item.image != null)
xSelectStartLoc = halfBorderWidth + IMAGE_WIDTH + 2;
else
xSelectStartLoc = halfBorderWidth;
if (item.bCellBorder)
gridAdj = 2;
else
gridAdj = 0;
if (item.bSelected)
{
g.drawImage(SelectImage, xCoord + xSelectStartLoc + gridAdj, yLoc, lWidth-xCoord, cellHt, this);
g.setColor(Color.white);
}
else
{
if (item.bEnabled)
{
if (item.color == null)
g.setColor(enabledColor);
else
g.setColor(item.color);
}
else
g.setColor(disabledColor);
}
g.drawString(item.sText, xCoord + xSelectStartLoc + gridAdj, ySLoc-fontDescent);
if (item.image != null)
g.drawImage(item.image, xCoord + halfBorderWidth + gridAdj + 2, yLoc+2, IMAGE_WIDTH, cellHt-3, this);
if (item.bCellBorder)
{
g.setColor(item.cellBorderColor);
g.drawRect(halfBorderWidth, yLoc, lWidth, cellHt);
}
}
extraPaint(g, rect);
bAllDirty = false;
}
/**
* Override this method to add any special painting effects after text and images are painted
* @param g the graphics
* @param rect the bounding rectangle
*/
public void extraPaint(Graphics g, Rectangle rect)
{
}
/**
* Makes this component visible.
* This is a standard Java AWT method which gets called to show this
* component. If this component was invisible due to a previous hide()
* call it make this component visible again.
*
* @see java.awt.Component#hide
*/
public synchronized void show()
{
bAllDirty = true;
super.show();
}
/**
* Moves and/or resizes this component.
* This is a standard Java AWT method which gets called to move and/or
* resize this component. Components that are in containers with layout
* managers should not call this method, but rely on the layout manager
* instead.
*
* @param x horizontal position in the parent's coordinate space
* @param y vertical position in the parent's coordinate space
* @param width the new width
* @param height the new height
*/
public synchronized void reshape(int x, int y, int width, int height)
{
bAllDirty = true;
super.reshape(x, y, width, height);
}
/**
* Set the list to be "dirty" to force a complete
* paint on next repaint.
*/
public synchronized void setDirty()
{
bAllDirty = true;
}
/**
* Query if all rows are selected.
* @return returns true if all rows in the ImageListBox
* are selected
*/
public boolean allSelected()
{
return (countSelected == items.size());
}
/**
* The ImageListBox posts LIST_SELECT and ACTION_EVENT events
* directly to a specific parent if one is specified. If one
* is not specified, the Event is posted directly to the parent
* container UNLESS the bDirectNotify notify flag is set to
* false.
* @param bDirectNotify true to directly notify parent with
* event, false to not directly notify
* (post event to self so extended classes can handle it)
*/
public void setDirectNotify(boolean bDirectNotify)
{
this.bDirectNotify = bDirectNotify;
}
/**
* Determines if the given index is valid.
* @param index a zero-relative index to check
* @return true if the index is valid, false if not
*/
protected boolean validIndex(int index)
{
if (index >= 0 && index < items.size())
return true;
return false;
}
/**
* Determines if the item referred to by the given index is visible.
* @param index the zero-relative index of the item to check
* @return true if the item is visible, false if not
*/
protected boolean isVisibleIndex(int index)
{
if (index >= nTopRow && index < (nTopRow + visibleRows))
return true;
return false;
}
/**
* Returns a string representing the state of this object.
* This is a standard Java AWT method which can be usefull for debugging.
*
* @return a handy string for debugging purposes
*/
protected String paramString()
{
return super.paramString() + ", selected=" + getSelectedItem();
}
/**
* Select an index in a multiple-selection-enabled ImageListBox
* using Shift or Ctrl-Shift modifiers to mimic mouse
* selecting with the Shift or Ctrl-Shift modifiers.
* @param index the zero-relative position of the item to select multiple
* @param bControl boolean indicating whether to select with
* Ctrl modifier
*/
protected void shiftSelect(int index, boolean bControl)
{
int i = 0;
int s = 0;
ListItem li;
if (lastSelected == -1)
select(index);
else if (lastSelected == index)
{
if (!bControl)
{
bInternalBlockPaint = true;
select(index);
bInternalBlockPaint = false;
}
}
else if (lastSelected < index)
{
if ( !isEnabled(index) )
return;
if (bControl)
{
i = lastSelected + 1;
s = Math.min(items.size(), index + 1);
}
else
{
i = 0;
s = items.size();
}
for (; i < s; i++)
{
li = (ListItem)items.elementAt(i);
if (i < lastSelected || i > index)
{
if (li.bSelected)
{
li.bDirty = true;
li.bSelected = false;
countSelected--;
}
}
else
{
if (!li.bSelected)
{
li.bDirty = true;
li.bSelected = true;
countSelected++;
}
}
}
}
else if (lastSelected > index)
{
if ( !isEnabled(index) )
return;
if (bControl)
{
i = index;
s = Math.min(items.size(), lastSelected);
}
else
{
i = 0;
s = items.size();
}
for (i = 0; i < s; i++)
{
li = (ListItem)items.elementAt(i);
if (i < index || i > lastSelected)
{
if (li.bSelected)
{
li.bDirty = true;
li.bSelected = false;
countSelected--;
}
}
else
{
if (!li.bSelected)
{
li.bDirty = true;
li.bSelected = true;
countSelected++;
}
}
}
}
if (!bInternalBlockPaint) repaint();
}
/**
* Select an index in a ImageListBox using the Ctrl modifier
* to mimic mouse selecting with the Ctrl modifier.
* @param index the zero-relative position of the item to select
*/
protected void ctrlSelect(int index)
{
ListItem listItem = (ListItem)items.elementAt(index);
if (!listItem.bEnabled)
return;
if (listItem.bSelected)
{
listItem.bSelected = false;
countSelected--;
lastSelected = -1;
}
else
{
listItem.bSelected = true;
countSelected++;
lastSelected = index;
}
listItem.bDirty = true;
if (!bInternalBlockPaint) repaint();
}
/**
* A utility routine that determines the item index the mouse is in
* given the y coordinate of the mouse.
* @param y mouse y coordinate
* @return the zero-relative index of the item the mouse is in
*/
protected int mouseCalcIndex(int y)
{
int mci;
if (y < yAdj)
mci = nTopRow - 1;
else
mci = ((y - yAdj) / cellHt) + nTopRow;
if (mci >= items.size())
mci = -1;
else if (mci < 0)
mci = 0;
return mci;
}
private void updateWidth(ListItem li)
{
int lineWidth = li.lineWidth + LINE_SLOP;
if (li.image != null)
lineWidth += IMAGE_WIDTH;
if ( lineWidth > longestLineValue)
longestLineValue = lineWidth;
}
private void updateWidths(FontMetrics fm)
{
longestLineValue = 0;
int s = items.size();
int lineWidth = 0;
ListItem li;
for (int x = 0; x < s; x++)
{
li = (ListItem)items.elementAt(x);
li.updateWidth(fm);
lineWidth = li.lineWidth + LINE_SLOP;
if (li.image != null)
lineWidth += IMAGE_WIDTH;
if ( lineWidth > longestLineValue)
longestLineValue = lineWidth;
}
}
private void notifyParent(Event evt, int index, boolean bDoubleClick)
{
Component postTo = null;
if (ilbParent != null)
postTo = ilbParent;
else if (bDirectNotify)
postTo = getParent();
if (postTo == null)
postTo = this;
if (bDoubleClick)
{
postTo.postEvent(new Event(this, evt.when, Event.ACTION_EVENT,
evt.x, evt.y, evt.key, lastDownModifiers, getSelectedItem()));
}
else
{
//inside image display area, and line has image!
if (evt.x >= 0 && (evt.x - xCoord) < IMAGE_WIDTH)
{
ListItem listItem = (ListItem)items.elementAt(index);
if (listItem.image != null)
{
postTo.postEvent(new Event(this, evt.when, EVT_IMAGE_SELECT,
evt.x, evt.y, evt.key, lastDownModifiers, new Integer(index)));
}
}
postTo.postEvent(new Event(this, evt.when, Event.LIST_SELECT,
evt.x, evt.y, evt.key, lastDownModifiers, new Integer(index)));
}
}
private int keyCalcIndex(int offset, boolean bAbsolute)
{
if (bAbsolute)
lastIndex = offset;
else
lastIndex += offset;
if (lastIndex >= items.size())
lastIndex = items.size() - 1;
else if (lastIndex < 0)
lastIndex = 0;
return lastIndex;
}
}